import { createSlice } from "@reduxjs/toolkit";
import {SecurityPath} from "type/entity/SecurityPath";
import {fetchAddPath, fetchPathPageData, fetchUpdatePath} from "../async-action/pathAsyncAction";
import RequestStatus from "../RequestStatus";
import { StatePagingType } from "..";
import { builderPagingHandler } from ".";
import UserStorage from "../../local/UserStorage";

export type PathState = StatePagingType<any>;
const initialState: PathState = {
    data: [],
    loading: false,
    requestStatus: RequestStatus.INIT,
    currentPage: 1,
    pageSize: UserStorage.getInstance().getAdminPageConfigFromLocalStore().pathPageSize,
    total: 0,
    totalPage: 5
};

const pathSlice = createSlice({
    name: 'path',
    initialState,
    reducers: {},
    extraReducers: (builder) => {
        builderPagingHandler<SecurityPath>(builder, fetchPathPageData);
        builder.addCase(fetchAddPath.fulfilled, (state, action) => {
            if (action.payload.success){
                state.requestStatus = RequestStatus.REFRESH;
            }
        });
        builder.addCase(fetchUpdatePath.fulfilled, (state, action) => {
            if (action.payload.success){
                state.requestStatus = RequestStatus.REFRESH;
            }
        });
    }
});

export const { } = pathSlice.actions;
export default pathSlice.reducer;